import axios from '../config/axiosConfig'; // 引入封装的 axios
/**
 * 文章分类接口返回类型
 */
export interface ArticleCategory {
	id : number;
	name : string;
	createdAt : string;
	updatedAt : string;
	articles : Article[];
}

/**
 * 文章接口返回类型
 */
export interface Article {
	id : number;
	title : string;
	summary : string | null;
	content : string | null;
	preview : number;
	categoryId : number;
	createdAt : string;
	updatedAt : string;
	category : ArticleCategory;
}
/**
 * 
 */
interface AddFromDataFace {
	inline : boolean;
	fields : {
		label : string;
		type : string;
		name : string;
		value : string | number;
		placeholder : string;
		width ?: string;
		options ?: ArticleCategory[];
		rules ?: {
			required : boolean;
			message : string;
			trigger : string;
		}[];
	}[];
}
export interface buttonData {
	label : string;
	type : string;
	value : string;
}
interface tableConfigDataFace {
	border : boolean;
	data : Article[];
	columns : {
		label : string;
		prop ? : string;
		align : string;
		type ?: string;
		width ?: string;
		button ? : buttonData[]
	}[]
}

/**
 * 添加所需的from表单格式
 */
export const addFormData : AddFromDataFace = {
	inline: false,
	fields: [{
		label: '文章标题',
		type: 'input',
		name: 'title',
		value: '',
		placeholder: '请输入标题',
		width:'70%',
		rules: [
			{ required: true, message: '请输入标题', trigger: 'blur' }
		]
	}, {
		label: '文章分类',
		type: 'select',
		name: 'categoryId',
		value: '',
		width:'70%',
		placeholder: '请选择分类',
		options: [],
		rules: [
			{ required: true, message: '请选择分类', trigger: 'change' }
		]
	}, {
		label: '文章简介',
		type: 'input',
		name: 'summary',
		value: '',
		width:'70%',
		placeholder: '请输入简介'
	}, {
		label: '文章内容',
		type: 'editor',
		name: 'content',
		value: '',
		placeholder: '请输入内容'
	}]
}

export const formDataData : AddFromDataFace = {
	inline: true,
	fields: [{
		label: '文章标题',
		type: 'input',
		name: 'title',
		value: '',
		placeholder: '请输入标题进行搜索',
		width:'350px'
	}, {
		label: '文章分类',
		type: 'select',
		name: 'categoryId',
		value: '',
		placeholder: '请选择分类',
		options: []
	}]
}


/**
 * tableConfigData
 */
export const tableConfigData : tableConfigDataFace = {
	border: false,
	data: [],
	columns: [
		{ label: 'ID', prop: 'id', align: 'center' },
		{ label: '标题', prop: 'title', align: 'center' },
		{ label: '分类', prop: 'category.name', align: 'center' },
		{ label: '摘要', prop: 'summary', align: 'center' },
		{ label: '创建时间', prop: 'createdAt', align: 'center' },
		{
			label: '操作',
			align: 'center',
			type: 'button',
			width: '200px',
			button: [
				{ label: '编辑', type: 'primary', value: 'edit' },
				{ label: '删除', type: 'danger', value: 'delete' }
			]
		}
	]
}



/**
 * 获取所有文章分类
 */
export const getCategoriesApi = () => {
	return axios.get('/article/categories');
};

/**
 * 创建文章分类
 */
export const createCategoryApi = (name : string) => {
	return axios.post('/article/categories', { name });
};

/**
 * 更新文章分类
 */
export const updateCategoryApi = (id : number, name : string) => {
	return axios.put(`/article/categories/${id}`, { name });
};

/**
 * 删除文章分类
 */
export const deleteCategoryApi = (id : number) => {
	return axios.delete(`/article/categories/${id}`);
};

/**
 * 获取文章列表
 */
export const getArticlesApi = (data ?: { page ?: number; pageSize ?: number }) => {
	return axios.get('/article/articles', { params: data });
};

/**
 * 获取单篇文章详情
 */
export const getArticleByIdApi = (id : number) => {
	return axios.get(`/article/articles/${id}`);
};

/**
 * 创建文章
 */
export const createArticleApi = (data : {
	title : string;
	summary : string | null;
	content : string | null;
	categoryId : number;
}) => {
	return axios.post('/article/articles', data);
};

/**
 * 更新文章
 */
export const updateArticleApi = (id : number, data : {
	title : string;
	summary : string | null;
	content : string | null;
	categoryId : number;
}) => {
	return axios.put(`/article/articles/${id}`, data);
};

/**
 * 删除文章
 */
export const deleteArticleApi = (id : number) => {
	return axios.delete(`/article/articles/${id}`);
};